package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* SquareAngularCurveView
* */
class SquareAngularCurveView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_D32F2F)
        }
    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            mPath.apply {
                reset()
                moveTo(0F, 0F)
                addArc(
                    -cx,
                    -cy,
                    0F,
                    0F,
                    180F,
                    90F,
                )
                lineTo(-cx, -cy)
            }
            drawPath(mPath, mPaint)

            mPath.apply {
                reset()
                moveTo(0F, 0F)
                addArc(
                   0F,
                    -cy,
                    cx,
                    0F,
                    270F,
                    90F,
                )
                lineTo(cx, -cy)
            }
            drawPath(mPath, mPaint)

            mPath.apply {
                reset()
                moveTo(0F, 0F)
                addArc(
                   0F,
                    0F,
                    cx,
                    cy,
                    0F,
                    90F,
                )
                lineTo(cx, cy)
            }
            drawPath(mPath, mPaint)

            mPath.apply {
                reset()
                moveTo(0F, 0F)
                addArc(
                   -cx,
                    0F,
                    0F,
                    cy,
                    90F,
                    90F,
                )
                lineTo(-cx, cy)
            }
            drawPath(mPath, mPaint)

            restore()
        }
    }
}